<?php

class TagSet
{
	private $Tags;
	
	private $firstId;
	private $lastId;
	
	// Constructor
	function __construct($Tags = false)
	{
		$this->Tags = array();
		
		if(is_array($Tags))
			$this->setTags($Tags);
	}
	
	##########################
	# Return a SPACE SEPARATED VALUES STRING with all the tags in the set
	# Param : 
	# Returns : String
	public function getFormatedList()
	{
		$res = "";
		foreach($this->getTags() as $tag)
			$res .= $tag->getTag()." ";
		$res = trim($res);
		return $res;
	}
	
	##########################
	# Check is this TagSet contains the given Tag object
	# Param : id OR name of the tag
	# Returns :
	public function hasTag($param)
	{
		foreach($this->getTags() as $tag)
			if($tag->getId() == $param or $tag->getTag() == $param)
				return true;
		return false;
	}
	
	##########################
	# Appends (= adds at the end) an instance of Note
	# Use : append($Tag)
	# Param : note object
	# Returns :
	public function append($Tag)
	{
		$this->Tags[] = $Tag;
		
		// Update last id
		$this->setLastId($Tag->getId());
		
		// Update first id if this Tag is the first to be added
		if($this->count() == 1)
			$this->setFirstId($Tag->getId());
	}
	
	##########################
	# Prepends (= adds at the beginning) an instance of Note
	# Use : prepend($Tag)
	# Param : note object
	# Returns :
	public function prepend($Tag) 
	{
		array_unshift($Tag,$this->Tags);
		
		// Update first id
		$this->setFirstId($Tag->getId());
		
		// Update last id is this note is the first to be added
		if($this->count() == 1)
			$this->setLastId($Tag->getId());
	}
	
	##########################
	# Gets the range of id of the Tags
	# Use : $array = $Tagset->getRange(); $first_id = $array[0]; $last_id = $array[1];
	# Param :
	# Returns : array
	public function getRange()
	{
		$range = array();
		
		if($this->getLastId() > $this->getFirstId())
		{
			$range[0] = $this->getFirstId();
			$range[1] = $this->getLastId();
		}
		else
		{
			$range[0] = $this->getLastId();
			$range[1] = $this->getFirstId();
		}
		
		return $range;
	}
	
	##########################
	# Gets the id of the FIRST note, whatever the sorting is (ASC or DESC)
	# Use : $first_id = $Tagset->getAbsoluteFirstId();
	# Param :
	# Returns : int
	public function getAbsoluteFirstId()
	{
		$range = $this->getRange();
		return $range[0];
	}
	
	##########################
	# Gets the id of the LAST note, whatever the sorting is (ASC or DESC)
	# Use : $last_id = $Tagset->getAbsoluteLastId();
	# Param :
	# Returns : int
	public function getAbsoluteLastId()
	{
		$range = $this->getRange();
		return $range[1];
	}
	
	
	##########################
	# Setters
	
	public function setTags($Tags)
	{
		if(!is_array($Tags))
		{
			throw new Exception('Argument must be an array of Tag instances');
			return;
		}

		$this->Tags = $Tags;
				
		// Set first and last id
		$this->setFirstId($this->Tags[0]->getId());
		$this->setLastId($this->Tags[$this->count()-1]->getId());
	}
	
	public function setLastId($id)
	{
		if(is_numeric($id))
			return $this->lastId = $id;
		else
			return false;
	}
	
	public function setFirstId($id)
	{
		if(is_numeric($id))
			return $this->firstId = $id;
		else
			return false;
	}
	
	##########################
	# Getters
	
	public function getTags()
	{
		return $this->Tags;
	}
	
	public function getFirstId()
	{
		return $this->firstId;
	}
	
	public function getLastId()
	{
		return $this->lastId;
	}
	
	public function count()
	{
		return count($this->Tags);
	}
	
}

?>
